<?php
namespace Admin\Controller;

use Think\Controller;

// 配对
class ProductProClassController extends Controller
{
	//商品与分类配对
	public function add()
	{
		$data = M('Product_product_class');
        // 右联商品表
		$prod = $data->join('RIGHT JOIN product ON product_product_class.product_id = product.id')->select();
		$this->assign('prod', $prod);
  		// 右联分类表
		$clas = $data->join('RIGHT JOIN product_class ON product_product_class.product_class_cid = product_class.cid')->select();
		$this->assign('clas', $clas);

		$this->display();
	}

	public function insert()
	{
		$active = M('Product_product_class');
			$active->create();
		$result = $active->add();
		if ($result) {
			$this->success('添加成功', 'view');
		} else {
			$this->error('添加失败');
		}
	}

	//用表格的方式显示数据
	public function view()
	{
		$data = M('Product_product_class'); //实例化对象
		// 查询满足要求的总记录数
		$count = $data->count();
		// 实例化分页类 传入总记录数和每页显示的记录数
		$Page = new \Think\Page($count, 20);
		
		$show = $Page->show(); //分页显示输出
		
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $data->join('RIGHT JOIN product ON product_product_class.product_id = product.id')->join('product_class ON product_product_class.product_class_cid = product_class.cid')->order('pid')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('list', $list);
		$this->assign('page', $show); //赋值分页输出 		
		$this->display(); //输出模板
	}
    

	//删除数据
	public function cancel()
	{
		$pid = $_GET['pid'];
		$action = M('Product_product_class');
		$result = $action->where('pid="'.$pid.'"')->delete();
		if ($result) {
			$this->success('删除失败');
		} else {
			$this->error('删除成功', 'view');
		}
	}
}